﻿using System;
using System.Linq;
using System.Web.UI;
using Bintje;
using System.Collections.Generic;

public partial class UI_AddTrail : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        try{
            if (!Page.IsPostBack && AppUser.currUser() == 0)
            {
                Response.Redirect("../Login.aspx");
            }
            if (!Page.IsPostBack && Request["TrailID"] != null)
            {
                
                int TrailID = Convert.ToInt32(Request["TrailID"].ToString());
                LoadTrail(TrailID);
                txtTitle.Text = Request["trailname"];
            }/*
            else if (Request["TrailID"] == null)
            {
                Response.Redirect("Trail.aspx");
            }*/
            DataBind();
        }
        catch (Exception ex) { }
    }

    public override void DataBind()
    {
        base.DataBind();
        IList<Bintje.Layer> layers = Bintje.Layer.GetLayers(Session["Session"] as Bintje.bintje.imageoSession);
        if (null != layers && layers.Count > 0)
        {
            var lrs = (from l in layers
                       orderby l.Id
                       select new { l.Id, l.LayerName }).ToArray();
            if (null != lrs)
            {
                chkLayers.DataSource = lrs;
                chkLayers.DataValueField = "Id";
                chkLayers.DataTextField = "LayerName";
                chkLayers.DataBind();
            }
        }
    }

    private void LoadTrail(int TrailID)
    {
        try
        {
            
            GMap1.TrailID = TrailID;
            Bintje.Trail trailInstance = GMap1.LoadTrail();

            if (null != trailInstance)
            {
                WayPoint waypoint = trailInstance.Waypoints[0];
                if (null != waypoint)
                {
                    txtTripDate.Text = waypoint.Created.ToString("MM/dd/yyyy");
                    if (null != waypoint)
                    {
                        txtCity.Text = waypoint.City;
                        txtCountry.Text = waypoint.Country;
                        txtState.Text = waypoint.Province;
                    }

                    if (null != trailInstance.Layers && trailInstance.Layers.Count > 0)
                        chkLayers.SelectedValue = trailInstance.Layers[0].Id.ToString();
                }

                txtTitle.Text = trailInstance.Title;
                ftbTripStory.Text = trailInstance.Description;
            }
        }
        catch (Exception ex) { }
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            Layer layer = null;/*
            if(!string.IsNullOrEmpty(txtNewLayer.Text))
            {
                layer = new Layer(txtNewLayer.Text);
                layer.Create(Session["Session"] as Bintje.bintje.imageoSession);
            }
            else
            {
                layer = new Layer(int.Parse(ddlLayers.SelectedItem.Value), ddlLayers.SelectedItem.Text);
            }
            */
            if(chkLayers.SelectedIndex > -1)
                layer = new Layer(int.Parse(chkLayers.SelectedItem.Value), chkLayers.SelectedItem.Text);
            //int TrailID = Convert.ToInt32(Request["TrailID"].ToString());
            DateTime? tripDate = null;
            if (!string.IsNullOrEmpty(txtTripDate.Text))
            {
                tripDate = DateTime.Parse(txtTripDate.Text);
            }
            //Bintje.Trail trail = Bintje.Trail.Get(TrailID);
            Bintje.Trail trail = GMap1.CollectTrailData();
            trail.Session = Session["Session"] as Bintje.bintje.imageoSession;
            try
            {
                if (null == trail.Waypoints || trail.Waypoints.Count == 0)
                    trail.Waypoints.Add(new WayPoint());
                if (!string.IsNullOrEmpty(txtCity.Text.Trim()))
                    trail.Waypoints[0].City = txtCity.Text.Trim();
                if (!string.IsNullOrEmpty(txtCountry.Text.Trim()))
                    trail.Waypoints[0].Country = txtCountry.Text.Trim();
                if (!string.IsNullOrEmpty(txtState.Text.Trim()))
                    trail.Waypoints[0].Province = txtState.Text.Trim();
                if(tripDate.HasValue)
                    trail.Waypoints[0].Created = tripDate.Value.ToBinary();
                if (null != layer)
                {
                    if (trail.Layers == null)
                        trail.Layers = new List<Layer>();
                    if (!trail.Layers.Contains(layer))
                        trail.Layers.Add(layer);
                }
                //trail = trail.KeepImages(trail);
                trail.TrailCreated = tripDate.Value;
            }
            catch (Exception){}
            trail.Description = ftbTripStory.Text.Trim();
            trail.Title = txtTitle.Text.Trim();
            //trail = trail.Update();
            if (GMap1.IsUpdateMode)
                trail = trail.Update();
            else
                trail = trail.Create();
            
            Response.Redirect("Home.aspx");
        }
        catch (Exception ex) { }

    }
}
